

/*
preferred habitat localization regressions
baseline

output:
- f3a_*.pdf: localization figure 

*/


* plotting options
set scheme lean_uncluttered

* command line option
* set to 1 to re-estimate PH regressions (slow)
if "`1'"=="" {
	local estimate_phregs 0
}
else {
	local estimate_phregs `1'
}


********************************************************************************
********************************************************************************
* prepare data
* all yield curve data for notes/bonds auctions
use ../data/auction_notesbonds_govpx_shock_all.dta, clear

* only use regularly traded securities
drop if when_issued
drop when_issued
* drop any maturity with less than 1 month until maturity
drop if maturity_remaining<30
* convert maturity remaining/lapsed into years
replace maturity_remaining = maturity_remaining/365
replace maturity_lapsed = maturity_lapsed/365

* winsorize govpx yields
winsor2 D_shock , replace cuts(.1 99.9) 

* drop if too few observations in govpx shocks
gen N_obs = N_obs_pre + N_obs_post
drop if N_obs<10

* create auction_id indicator
sort close_time maturity_remaining cusip_govpx
egen auction_id = group(close_time)


* auction terms: indicator for short/long auctions
* 2-5 year and 7-30 year auctions
local trm_cut 6
gen term_long = term_num>`trm_cut'

* high risk aversion period: 2008-2012
gen high_ra = (auctionDate>=mdy(1,1,2008) & auctionDate<=mdy(12,31,2012))

save ../data/phreg_clean_auction_notesbonds_govpx_shock_all.dta, replace


********************************************************************************
********************************************************************************
* estimate
if `estimate_phregs' == 1 {

use ../data/phreg_clean_auction_notesbonds_govpx_shock_all.dta, clear

* run PH regressions
n di "running localization regression"
run_ph_regression D_shock , ///
	mat_var(maturity_remaining) ra_var(high_ra) long_var(term_long) ///
	security_id(cusip_govpx) auction_id(auction_id) ///
	tau_star(3) star_window(2) mat_window(2) mat_long_window(4) mat_step(1) ///
	cluster_type("NT") bw(9) ///
	fname_stub("baseline")

}

********************************************************************************
********************************************************************************
* plot
plot_ph_regression, fname_stub("baseline") ///
	show_figs(0) figname("f3a_ph_regression_baseline") combine_figs(0)
plot_ph_regression, fname_stub("baseline") ///
	show_figs(0) figname("f3a_ph_regression_baseline") combine_figs(1)




